package com.geekchique.runningbuddy.view.overlay;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.location.Location;
import android.view.View;

import com.geekchique.runningbuddy.activity.MapOfJogs;
import com.google.android.maps.Overlay;
import com.google.android.maps.Point;

public class MapOfJogsOverlay  extends Overlay {

	private final android.graphics.Point PIN_HOTSPOT = new android.graphics.Point(5,29);
	private final MapOfJogs mapOfJogActivity;

	@SuppressWarnings("unused")
	protected final Bitmap PIN_START;
	protected final Bitmap PIN_END;
	private PixelCalculator pixelCalculator;
	private int[] prevScreenCoords;
    public List<Location> path = new ArrayList<Location>();
	int[] screenCoords = {0,0};
	private Canvas canvas;
	boolean firstInPath;
	boolean first = true;

	public MapOfJogsOverlay(MapOfJogs map, Bitmap startLocationImage, Bitmap endLocationImage, Bitmap infoImageBG) {
		mapOfJogActivity = map;
		PIN_START = startLocationImage;
		PIN_END = endLocationImage;

		prevScreenCoords = new int[2];
	}

	public void draw(Canvas canvas, PixelCalculator pixelCalc, boolean withShadows) {
		super.draw(canvas, pixelCalc, withShadows);
		this.canvas = canvas;
		this.pixelCalculator = pixelCalc;

		int entry = 1;

		for (Location loc : path) {
			 screenCoords = getPointOnMap((int) loc.getLatitude(), (int) loc.getLongitude());

			if(entry == 1){
				drawFirstPoint(screenCoords[0], screenCoords[1]);
				entry++;
			}else{
				drawLineToPoint(screenCoords[0], screenCoords[1]);
			}

		}

		dropPinMarker(canvas, PIN_START, screenCoords[0] -3, screenCoords[1] + 20);

	}

	View.OnClickListener GoClickListener = new View.OnClickListener() {

		public void onClick(View arg0) {
			mapOfJogActivity.finish();
		}
	};

	public void drawFirstPoint(int lat, int longi){
		dropPinMarker(canvas, PIN_END, lat, longi - 18);
		canvas.drawOval(new RectF(lat - 5, longi + 5, lat + 5, longi - 5), getJoggingPoint());

		prevScreenCoords[0] = lat;
		prevScreenCoords[1] = longi;
	}

	public void drawLineToPoint(int lat, int longi){
		canvas.drawOval(new RectF(lat - 5, longi + 5, lat + 5, longi - 5), getJoggingPoint());
		canvas.drawLine(prevScreenCoords[0], prevScreenCoords[1], lat, longi, getJoggingPathPainter());

		prevScreenCoords[0] = lat;
		prevScreenCoords[1] = longi;
	}

	private Paint getJoggingPoint() {
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setARGB(80, 156, 192, 36);
		paint.setStrokeWidth(3);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		return paint;
	}

	private Paint getJoggingPathPainter() {
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setARGB(80, 255, 0, 0);
		paint.setStrokeWidth(3);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		return paint;
	}

	private int[] getPointOnMap(int lat, int longi) {
		Point point = new Point(lat, longi);
		int[] screenCoords = new int[2];
		pixelCalculator.getPointXY(point, screenCoords);
		return screenCoords;
	}

	private void dropPinMarker(Canvas canvas, Bitmap image, int lat, int longi) {
		canvas.drawBitmap(
				image,
				lat - PIN_HOTSPOT.x,
				longi - PIN_HOTSPOT.y,
				getRoutePathPainter());
	}


	protected Paint getJourneyTimePainter(){
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setARGB(255,255,255,255);
		paint.setStrokeWidth(1);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setTextSize(24);
		return paint;
	}

	protected Paint getJourneyDistPainter(){
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setARGB(255,255,255,255);
		paint.setStrokeWidth(1);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setTextSize(16);
		return paint;
	}

	protected Paint getRoutePathPainter(){
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStrokeWidth(4);
		paint.setARGB(100, 113, 105, 252);
		paint.setStyle(Paint.Style.STROKE);
		return paint;
	}

	public void addLocationToPath(Location pointLocation) {
		path.add(pointLocation);
	}

}

